package pl.streamsoft.currencyApp.services;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import pl.streamsoft.currencyApp.dao.CountryDAO;
import pl.streamsoft.currencyApp.dao.CurrencyDAO;
import pl.streamsoft.currencyApp.dao.ICountryDAO;
import pl.streamsoft.currencyApp.dao.ICurrencyDAO;
import pl.streamsoft.currencyApp.model.Country;
import pl.streamsoft.currencyApp.model.Currency;
import pl.streamsoft.currencyApp.parser.XMLCurrencyParser;

public class AppService implements IAppService {

	ICurrencyDAO currencyDAO;
	ICountryDAO countryDAO;
	CurrencyService currencyService;

	// PRZETESTOWAĆ!!!!!!!

	public AppService() throws MalformedURLException, IOException {
		currencyDAO = new CurrencyDAO();
		countryDAO = new CountryDAO();
		currencyService = new CurrencyService(new XMLCurrencyParser());

	}

	public void addCountry(String name){
		if(getCountry(name)==null){
			Country country = new Country();
			country.setName(name);
			countryDAO.insertCountryIntoDB(country);
		}
	}

	public void addCurrencyToCountry(String countryName, Currency currency) throws RuntimeException {
		Country country = getCountry(countryName);
		if (country != null) {
	
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	        String date2 = formatter.format(currency.getDate());
	        
			for(Currency el: country.getCurrencyList()){
				if(el.getDate().toString().equals(date2) && el.getName().equals(currency.getName())){
					throw new RuntimeException("Country alredy have a this currency!");
				}
			}
			
			country.getCurrencyList().add(currency);
			countryDAO.insertCountryIntoDB(country);
		}
		else{
			throw new RuntimeException("Can't get country from database!");
		}

	}

	public List<Currency> getAllCurrenciesOfCountry(String nameOfCountry){
		return countryDAO.getAllCountryCurrencies(nameOfCountry);
	}

	public List<Currency> getAllCurrenciesFromDateByType(Date date, char type){
		List<Currency> res = currencyDAO.getCurrenciesFromDate(date);
		if (res.isEmpty()) {
			res = currencyService.getListOfCurrenciesFromDate(date, type);
			if (!res.isEmpty()) {
				// check if currencies are in db
				Date dateFromNewCurrency = res.get(0).getDate();
				List<Currency> res2 = currencyDAO.getCurrenciesFromDate(dateFromNewCurrency);

				if (!res2.isEmpty()) {
					return res2;
				} else {
					currencyDAO.saveAllCurrencies(res);
					return res;
				}
			} else {
				return null;
			}
		} else {
			return res;
		}
	}

	public void addExistingCurrencyInDBToCountryByCodeAndDate(String currencyCode, Date date, String countryName){
		Currency currency = currencyDAO.getCurrencyFromDBByCodeAndDate(currencyCode, date);
		Country country = countryDAO.getCountryFromDB(countryName);
		country.getCurrencyList().add(currency);
		countryDAO.insertCountryIntoDB(country);
		//currencyDAO.addCurrencyIntoCountry(countryName, currency);  

	}

	public Country getCountry(String name){
		return countryDAO.getCountryFromDB(name);
	}

	@Override
	public List<Currency> getAllCurrenciesFromInterval(String from, String to){
		return currencyDAO.getAllCurrenciesFromInterval(from, to);
	}

	@Override
	public List<Currency> getAllCurrenciesMatchesWord(String word){
		return currencyDAO.getAllCurrenciesWhereNameMatchesWord(word);
	}

	@Override
	public Currency getCurrencyFromDateByCode(Date date, String currencyCode){
		Currency currencyFromDateByCode = currencyDAO.getCurrencyFromDBByCodeAndDate(currencyCode, date);

		if (currencyFromDateByCode == null) {
			getAllCurrenciesFromDateByType(date, 'a');
			currencyFromDateByCode = currencyDAO.getCurrencyFromDBByCodeAndDate(currencyCode, date);
		}
		return currencyFromDateByCode;
	}

	@Override
	public List<Currency> getAllCurrenciesFromDateToDate(Date from, Date to){

		return currencyDAO.getCurrencyFromDateToDate(from, to);
	}

	@Override
	public void addNewCurrencyToDB(Currency currency){
		currencyDAO.insertCurrencyIntoDB(currency);
	}
	//funkcjonalnosc pobrania country gdzie jest podana liczba currency przez uzytkownika

	@Override
	public void deleteCurrencyFromCountry(String countryName, Currency currency) {
		currencyDAO.deleteCurrencyFromCountry(countryName, currency);
	}
}
